﻿//remember that uVar type 1 = blue, 0 = red.

var myType:Number;
var playerX:Number;
var playerY:Number;

var scaledRingRadius:Number = 0;

var sortedAntArray:Array = null;
var sortedFoodArray:Array = null;

/**
myAntAvatar - the player's avatar
userList - list of users in the room
foodList - list of food objects in the room
*/

updateSensing = function(myAntAvatar:MovieClip, userArray:Array, foodArray:Array, 
						 home:MovieClip, playerType:Number, sphereVersion:Number){
	playerX = myAntAvatar._x;//_global.frameX + myAntAvatar._x;
	playerY = myAntAvatar._y;//_global.frameY + myAntAvatar._y;
	
	if(sortedAntArray == null || (sortedAntArray.length < userArray.length)){
		//trace("sortedAnt null");
		sortedAntArray = copyAndSortArray(userArray, antDistanceSort);	
	} else {
		sortedAntArray.sort(antDistanceSort);
	}
	if(sortedFoodArray == null){
//		trace("sortedFood null");
		sortedFoodArray = copyAndSortArray(foodArray, foodDistanceSort);
	} else {
		sortedFoodArray.sort(foodDistanceSort);
	}

	scaledRingRadius = RING_RADIUS * (SCALE/100);
	
	myType = playerType;
		
	var elementsShown:Number = 0;
	
//	clearSensingElements(myAntAvatar._parent);
	
	attachRing(myAntAvatar);
	
	for( i = 0; i < sortedAntArray.length; i++){
		var antUser:User = sortedAntArray[i];
		var uName:String = antUser.getName();
	
		if (uName != _global.myName) {
			var uVars:Object = antUser.getVariables();
			var otherX:Number = uVars.px - _global.frameX;
			var otherY:Number = uVars.py - _global.frameY;
			
			var distanceX:Number = otherX - playerX;
			var distanceY:Number = otherY - playerY;
			
			var hypo = Math.sqrt( Math.pow(distanceX, 2) + Math.pow(distanceY,2));
						
			if((hypo < MAX_SENSING_DISTANCE) && (elementsShown < MAX_ITEMS) && (hypo > MIN_SENSING_DISTANCE)){
				//at this point we are good to show something.
				addAntElement(myAntAvatar, elementsShown, uVars, 
							  hypo/MAX_SENSING_DISTANCE, hypo/scaledRingRadius);	
				elementsShown++;
			}
		}
	}
	for(;elementsShown < MAX_ITEMS; elementsShown++){
		avatarMC[ANT_PREFIX + elementsShown].removeMovieClip();
	}

	elementsShown = 0;
	for( var i:Number = 0; i < sortedFoodArray.length; i++){
		var foodX:Number = sortedFoodArray[i].xPos -_global.frameX;
		var foodY:Number = sortedFoodArray[i].yPos -_global.frameY;
			
		var distanceX:Number = foodX - playerX;
		var distanceY:Number = foodY - playerY;
			
		var hypo = Math.sqrt( Math.pow(distanceX, 2) + Math.pow(distanceY,2));
			
		if((hypo < MAX_SENSING_DISTANCE) && (elementsShown < MAX_ITEMS) && (hypo > MIN_SENSING_DISTANCE)){
			//at this point we are good to show something.
			addFoodElement(myAntAvatar, elementsShown, foodX, foodY,
						  hypo/MAX_SENSING_DISTANCE);	
			elementsShown++;
		}
	}	
	for(;elementsShown < MAX_ITEMS; elementsShown++){
		avatarMC[FOOD_PREFIX + elementsShown].removeMovieClip();
	}
	
	updateHome(myAntAvatar, home);
}

//----------------------------------------------------


//----------------------------------------------------

addAntElement = function(myAntAvatar:MovieClip, elementsShown:Number, uVars:Object, 
						 ratio:Number, ringRatio:Number) {
	var otherX:Number = uVars.px - _global.frameX;
	var otherY:Number = uVars.py - _global.frameY;
	var distanceX:Number = otherX - playerX;
	var distanceY:Number = otherY - playerY;
			
	var angle:Number = Math.atan2(distanceY, distanceX);
	
	var newX:Number = Math.cos(angle) * scaledRingRadius;
	var newY:Number = Math.sin(angle) * scaledRingRadius;
	
	//create and attach element
	var newElement:MovieClip;
	
	if (uVars.antType == ANT_TYPE_BLUE) {
			newElement = myAntAvatar._parent.attachMovie("sensingAntBlueSolid", ANT_PREFIX + elementsShown, 
											  ANT_LAYER_START + elementsShown);
	} else {
			newElement = myAntAvatar._parent.attachMovie("sensingAntRedSolid", ANT_PREFIX + elementsShown, 
											  ANT_LAYER_START + elementsShown);
	}
	
	newElement._x = myAntAvatar._x + newX;
	newElement._y = myAntAvatar._y + newY;
	
	var scaleValue:Number = (1 - ratio);
	if(scaleValue < MINIMUM_SCALING)
		scaleValue = .3;
	
	newElement._xscale = scaleValue * 100;
	newElement._yscale = scaleValue * 100;
	
	newElement._alpha = SENSING_ALPHA;
	
//	var newColor:Color = new Color(newElement);			
//	setBrightness(newColor, (50 * ratio));
	
	//set the element's location
}

//----------------------------------------------------
//				addFoodElement
//----------------------------------------------------

addFoodElement = function(myAntAvatar:MovieClip, elementsShown:Number, foodX:Number, foodY:Number, 
						  ratio:Number) {	
	//determine location of sensing element
	var distanceX:Number = foodX - playerX;
	var distanceY:Number = foodY - playerY;
	
	var angle:Number = Math.atan2(distanceY, distanceX);
	
//	var elementDistance:Number = scaledRingRadius * ratio;
	
	var newX:Number = Math.cos(angle) * scaledRingRadius;
	var newY:Number = Math.sin(angle) * scaledRingRadius;
	
	var newElement:MovieClip = myAntAvatar._parent.attachMovie("sensingFood", FOOD_PREFIX + elementsShown, 
															(FOOD_LAYER_START + elementsShown));
	newElement._x = myAntAvatar._x + newX;
	newElement._y = myAntAvatar._y + newY;
	
	var scaleValue:Number = (1 - ratio);
	if(scaleValue < MINIMUM_SCALING)
		scaleValue = .3;
	
	newElement._xscale = scaleValue * 100;
	newElement._yscale = scaleValue * 100;
	
	newElement._alpha = SENSING_ALPHA;
		
//	var newColor:Color = new Color(newElement);			
//	setBrightness(newColor, (50 * ratio));
}

//----------------------------------------------------
//					updateHome
//----------------------------------------------------

updateHome = function(myAntAvatar:MovieClip, home:MovieClip) {
	
	
	//userList_lb.addItem(uName, uId)
	var distanceX:Number = home._x - myAntAvatar._x;
	var distanceY:Number = home._y - myAntAvatar._y;
	
	var hypo:Number = Math.sqrt( Math.pow(distanceX, 2) +  Math.pow(distanceY,2));
	
	var angle:Number = Math.atan2(distanceY, distanceX);
	
	var newX:Number = Math.cos(angle) * scaledRingRadius;
	var newY:Number = Math.sin(angle) * scaledRingRadius;
	
	var homing:MovieClip;
	
	if(hypo > MIN_SENSING_DISTANCE){
		if(myType == ANT_TYPE_BLUE){
			homing = myAntAvatar._parent.attachMovie("homingBlue", "homing", HOMING_LAYER);
		} else {
			homing = myAntAvatar._parent.attachMovie("homingRed", "homing", HOMING_LAYER);
		}
	} else {
		myAntAvatar._parent["homing"].removeMovieClip();
	}
	
	homing._x = myAntAvatar._x + newX;
	homing._y = myAntAvatar._y + newY;
//	homing._rotation = angle*(180/Math.PI) + 90;
	
	homing._xscale = SCALE;
	homing._yscale = SCALE;
	
	homing._alpha = SENSING_ALPHA;
}

// helpful function from robert penner:
// http://robertpenner.com/scripts/color_extensions.txt

// brighten an object just like Effect panel
// bright between 0 and 100
setBrightness = function (colorObject:Color, bright) {
	var offset = 0;
	if (bright > 0) offset = 256 * (bright / 100);
	var trans = new Object();
	trans.ra = trans.ga = trans.ba = 100 - Math.abs(bright);
	trans.rb = trans.gb = trans.bb = offset;
	colorObject.setTransform(trans);
} //rp

//----------------------------------------------------

//----------------------------------------------------

clearSensingElements = function(avatarMC:MovieClip){
	for(var i:Number = 0; i < MAX_ITEMS; i++){
		for (var mc:String in avatarMC)
		{
			if (avatarMC[mc]._name == (ANT_PREFIX + i) | avatarMC[mc]._name == (FOOD_PREFIX + i)) {
				avatarMC[mc].removes();
			}
		}
	}
}

function attachRing(myAntAvatar:MovieClip){
	if(myAntAvatar._parent[RING_NAME] == null){
		var ring:MovieClip = myAntAvatar._parent.attachMovie("sensingRing", RING_NAME, 700);
		ring._x = myAntAvatar._x;
		ring._y = myAntAvatar._y;
		
		ring._xscale = SCALE;
		ring._yscale = SCALE;
		
		ring._alpha = SENSING_ALPHA;
	}
}


function antDistanceSort(A, B){
	var uVars_1:Object = A.getVariables();
	var otherX_1:Number = uVars_1.px - _global.frameX;
	var otherY_1:Number = uVars_1.py - _global.frameY;
			
	var distanceX_1:Number = otherX_1 - playerX;
	var distanceY_1:Number = otherY_1 - playerY;
			
	var hypot1 = Math.sqrt( Math.pow(distanceX_1, 2) + Math.pow(distanceY_1,2));
	
	var uVars_2:Object = B.getVariables();
	var otherX_2:Number = uVars_2.px - _global.frameX;
	var otherY_2:Number = uVars_2.py - _global.frameY;
			
	var distanceX_2:Number = otherX_2 - playerX;
	var distanceY_2:Number = otherY_2 - playerY;
			
	var hypot2 = Math.sqrt( Math.pow(distanceX_2, 2) + Math.pow(distanceY_2,2));
	
	if(hypot1 < hypot2){
		return -1;
	}else if (hypot1 > hypot2){
		return 1;
	}else {
		return 0;
	}
}

function foodDistanceSort(A, B){
	var foodX_1:Number = A.xPos -_global.frameX;
	var foodY_1:Number = A.yPos -_global.frameY;
			
	var distanceX_1:Number = foodX_1 - playerX;
	var distanceY_1:Number = foodY_1 - playerY;
			
	var hypot1:Number = Math.sqrt( Math.pow(distanceX_1, 2) + Math.pow(distanceY_1, 2));
	
	var foodX_2:Number = B.xPos -_global.frameX;
	var foodY_2:Number = B.yPos -_global.frameY;
			
	var distanceX_2:Number = foodX_2 - playerX;
	var distanceY_2:Number = foodY_2 - playerY;
			
	var hypot2:Number = Math.sqrt( Math.pow(distanceX_2, 2) + Math.pow(distanceY_2, 2));
	
	if(hypot1 < hypot2){
		return -1;
	}else if (hypot1 > hypot2){
		return 1;
	}else {
		return 0;
	}
}

function copyAndSortArray(oldArray:Array, sortFunction):Array{
	var newArray:Array = new Array();
	
	for(var i:Number =0; i < oldArray.length; i++){
		newArray.push(oldArray[i]);
	}
	
	newArray.sort(sortFunction);
	
	return newArray;
}